package com.hand.wfive.ad;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.DislikeInfo;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.hand.wfive.App;
import com.hand.wfive.loginAndVip.UserManager;
import com.hand.wfive.loginAndVip.ui.LoginIndexActivity;
import com.hand.wfive.loginAndVip.ui.VipCenterActivity;

import org.greenrobot.eventbus.EventBus;

import java.lang.ref.WeakReference;
import java.util.List;

public class AdManager {

    private final String TAG = AdConfig.AD_LOG_TAG;

    private static WeakReference<AdManager> mAdManager = new WeakReference<>(null);

    private Activity mActivity = null;
    private TTAdNative mTTAdNative = null;

    private AdManager() {
        if (TTAdManagerHolder.isInitAndReady()) {
            mTTAdNative = TTAdManagerHolder.get().createAdNative(App.getContext());
        }
    }

    public AdManager setActivity(Activity activity) {
        this.mActivity = activity;
        return this;
    }

    public static AdManager getInstance() {
        AdManager adManager = mAdManager.get();
        if (adManager == null) {
            adManager = new AdManager();
            mAdManager = new WeakReference<>(adManager);
        }
        if (adManager.mTTAdNative == null) {
            if (TTAdManagerHolder.isInitAndReady()) {
                adManager.mTTAdNative = TTAdManagerHolder.get().createAdNative(App.getContext());
            }
        }
        return adManager;
    }

    public void showBannerBottom(ViewGroup container) {
        if (!AdConfig.isShowAd || AdConfig.adDisable || mTTAdNative == null ||
                mActivity == null || mActivity.isFinishing()) {
            return;
        }
        float expressViewWidth = UIUtils.getScreenWidthDp(mActivity);
        float expressViewHeight = (float) (expressViewWidth * 0.15);
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(AdConfig.getBannerCodeId()) //广告位id
                .setSupportDeepLink(true)
                .setAdCount(3) //请求广告数量为1到3条
                .setExpressViewAcceptedSize(expressViewWidth, expressViewHeight) //期望模板广告view的size,单位dp
                .build();
        mTTAdNative.loadBannerExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            //请求失败回调
            @Override
            public void onError(int code, String message) {
                Log.d(TAG, "showBannerBottom code==" + code + ", message = " + message);
            }

            //请求成功回调
            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
                if (ads.get(0) == null) {
                    return;
                }
                final TTNativeExpressAd ad = ads.get(0);
                ad.setSlideIntervalTime(15 * 1000);
                ad.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
                    @Override
                    public void onAdClicked(View view, int i) {

                    }

                    @Override
                    public void onAdShow(View view, int i) {

                    }

                    @Override
                    public void onRenderFail(View view, String s, int i) {

                    }

                    @Override
                    public void onRenderSuccess(View view, float v, float v1) {
                        if (container != null) {
                            container.removeAllViews();
                            container.addView(view);
                        }
                    }
                });

                final DislikeInfo dislikeInfo = ad.getDislikeInfo();
                if (dislikeInfo != null && dislikeInfo.getFilterWords() != null && !dislikeInfo.getFilterWords().isEmpty()) {
                    if (mActivity != null && !mActivity.isFinishing()) {
                        final DislikeDialog dislikeDialog = new DislikeDialog(mActivity, dislikeInfo);
                        dislikeDialog.setOnDislikeItemClick(filterWord -> {
                            //用户选择不喜欢原因后，移除广告展示
                            container.removeAllViews();
                        });
                        ad.setDislikeDialog(dislikeDialog);
                    }
                }
                ad.render();
            }
        });
    }

    public void doVipIntent(Context context) {
        if (context == null) {
            adClose();
            return;
        }
        if (!UserManager.getInstance().isLogin()) {
            LoginIndexActivity.show(context, false);
        } else {
            if (UserManager.getInstance().isVip() || AdConfig.adDisable) {
                adClose();
            } else {
                VipCenterActivity.show(context);
                // 当前快专常的包一般不会有半屏阻拦弹窗了都统一跳会员中心
                /*VipConfigModel vipConfigModel = VipData.getVipGoodsModel();
                if (vipConfigModel == null || vipConfigModel.getObj().isEmpty() || vipConfigModel.getIsOpen() == 0) {
                    VipCenterActivity.show(context);
                } else {
                    VipPayActivity.show(context);
                }*/
            }
        }
    }

    private void adClose() {
        EventBus.getDefault().post(new ADCloseEvent());
    }
}
